<?php
declare(strict_types=1);

namespace Modules\Production\Http\Controllers;

use Catch\Base\CatchController as Controller;
use App\Services\InventoryService;
use App\Services\RedisLockService;use Illuminate\Http\Request;
use Modules\Production\Http\Requests\SockConfirmRequest;use Modules\Production\Http\Requests\SockTaskRequest;
use Modules\Production\Repository\ProductionOrderStockUserRepository;
use App\Enums\InvTransDocType;
use Modules\Production\Repository\StockTaskRepository;
use Modules\Production\Service\TaskScanService;

/**
 * 备货任务
 * Class StockTaskController
 * @package Modules\Production\Http\Controllers
 */
class StockTaskController extends Controller
{

    public function __construct(){

    }

    /**
     * 备货任务指派
     * @param Request $request
     * @return mixed
     */
    public function bindOperator(SockTaskRequest $request)
    {
        return TaskScanService::bindOperator($request->post());
    }

    /**
     * 金蝶备货预留查询
     * @param Request $request
     * @return array
     * @throws Exception
     */
    public function kingdeeStock($docno,$fnumber,Request $request){

        return [
            'stock' => StockTaskRepository::kingdeeStock($docno,$fnumber),
            'inv'   => StockTaskRepository::stockInv($docno,$fnumber)
        ];
    }

    /**
     * 备货任务扫描
     * @param $lotnum
     * @param Request $request
     */
    public function stockConfirm(SockConfirmRequest $request){
        return TaskScanService::materialScan($request->all());
    }

    /**
     * 备货提交
     * @param Request $request
     */
    public function stockSubmit(Request $request){

        $productionId = $request->post('production_id',0);

        $lockTag = 'PICK_SUBMIT_' . $productionId;
        $redisLock = new RedisLockService($lockTag,30);
        if(!$redisLock->lock()){
            throw new \Exception('当前工单备货提交处理中，请稍后重试！');
        }

        try{
            return TaskScanService::scanSubmit($request->post());
        }catch (\Exception $e){
            throw $e;
        } finally {
            //释放锁
            $redisLock->unlock();
        }
    }


}
